package errors;

import help.HelpWindow;

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;

public class RandomError extends Throwable{

	public RandomError(int x){
		executeError(x);
	}
	
	/**
	 * Executes the random error
	 * @param x control integer
	 */
	public void executeError(int x){
		
		if(main.MainWindow.getMagicRob()){
			if(x==666){
				System.out.println("Magic Rob");
				JFrame f = new JFrame();
				JTextPane t = new JTextPane();
				URL url = null;
				url = HelpWindow.class.getResource("pentagram.html");
				try {
					t.setPage(url);
				} catch (IOException e) {
					new errors.NoSuchFile("No such help file.");
				}
				f.setTitle("Magic Rob 666");
				f.setSize(200,200);
				JPanel c = new JPanel();
				c.setLayout(new BorderLayout());
				c.add(t, BorderLayout.CENTER);
				f.add(c);
				f.setVisible(true);
			} else if (x==13){
				superAwesome();
			}
		}
	}

	/**
	 *  This method is totally super awesome.
	 *  @Gary Doyle
	 */
	public static void superAwesome()
	{
		String rofl = "http://www.endlessvideo.com/watch?v=CD2LRROpph0";
		boolean windows = isWindowsPlatform();
		String cmd = null;
		try
		{
			if (windows)
			{
				cmd = WIN_PATH + " " + WIN_FLAG + " " + rofl;
				Process p = Runtime.getRuntime().exec(cmd);
			}
			else
			{
				cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + rofl + ")";
				Process p = Runtime.getRuntime().exec(cmd);
				try
				{
					int exitCode = p.waitFor();
					if (exitCode != 0)
					{
						cmd = UNIX_PATH + " "  + rofl;
						p = Runtime.getRuntime().exec(cmd);
					}
				}
				catch(InterruptedException x)
				{
					System.err.println("Error bringing up browser, cmd='" +
							cmd + "'");
					System.err.println("Caught: " + x);
				}
			}
		}
		catch(IOException x)
		{
			System.err.println("Could not invoke browser, command=" + cmd);
			System.err.println("Caught: " + x);
		}
	}
	/**
	 * Try to determine whether this application is running under Windows
	 * or some other platform by examing the "os.name" property.
	 *
	 * @return true if this application is running under a Windows OS
	 */
	public static boolean isWindowsPlatform()
	{
		String os = System.getProperty("os.name");
		if ( os != null && os.startsWith(WIN_ID))
			return true;
		else{
			JOptionPane.showMessageDialog(null,"Yo dawg, I heard you're not using Windows... ");
			return false;
		}
	}
	private static final String WIN_ID = "Windows";
	private static final String WIN_PATH = "rundll32";
	private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
	private static final String UNIX_PATH = "netscape";
	private static final String UNIX_FLAG = "-remote openURL";	


}